package com.chance.cc.crawler.development.bootstrap.jiuxian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequest;
import com.chance.cc.crawler.core.downloader.http.HttpClientDownloader;
import com.chance.cc.crawler.core.downloader.http.HttpClientGenerator;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.downloader.proxy.SiteConfigProxyProvider;
import com.chance.cc.crawler.core.selector.Html;
import com.chance.cc.crawler.core.selector.Json;
import com.chance.cc.crawler.core.selector.Selectable;
import groovy.util.logging.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.chance.cc.crawler.core.script.crawler.CrawlerScript.$_type;

/**
 * @author bx
 * @date 2021/1/8 0008 15:37
 */
@Slf4j
public class JiuXianHttpTest {
    public static Logger logger = LoggerFactory.getLogger(JiuXianHttpTest.class);

    private static Proxy proxy = new Proxy();
    static {
        //代理配置
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("H5168QRFNIU3804D");
        proxy.setPassword("5F6B3610BB719FAA");
    }

    public static void main(String[] args) {

        HttpConfig httpConfig = HttpConfig.me("test");
//        httpConfig.setProxy(proxy);
//        httpConfig.setUseProxy(true);

        HttpClientDownloader downloader = new HttpClientDownloader();
        downloader.setClientGenerator(new HttpClientGenerator());
        downloader.setProxyProvider(new SiteConfigProxyProvider());

        for (int i=1;i<=30;i++){
            new Thread(new TestCommentRun(downloader,httpConfig),"thread_"+i).start();
        }
    }

    static class TestRun implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestRun(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            String url = "https://newapplist.jiuxian.com/search/searchProduct.htm?deviceType=ANDROID&keyword=%E8%8C%85%E5%8F%B0&orderBy=1%3A0&#pageIndex&pageSize=10";
            HttpRequest httpRequest = new HttpRequest();

            for(int i=1;i<100;i++){
                httpRequest.setUrl(url.replace("#pageIndex",String.valueOf(i)));
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);
                JSONObject pageObj = JSONObject.parseObject(httpPage.getRawText());
                JSONObject result = pageObj.getJSONObject("result");
                JSONArray resultList = result.getJSONArray("resultList");
                System.out.println(resultList.size());
//                System.out.println(s);
            }
        }
    }

    static class TestArticleRun implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestArticleRun(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            String url = "https://newappproduct.jiuxian.com/product/proDetail.htm?deviceType=ANDROID&proId=9324";
            HttpRequest httpRequest = new HttpRequest();

            for(int i=0;i<100;i++){
                httpRequest.setUrl(url);
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);
                JSONObject pageObj = JSONObject.parseObject(httpPage.getRawText());
                String proName = pageObj.getJSONObject("result").getString("proName");
                System.out.println(proName);

            }
        }
    }

    static class TestCommentRun implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestCommentRun(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            String url = "https://newappuser.jiuxian.com/comment/getProductCommentDetail.htm?appVersion=8.8.0&deviceType=ANDROID&pager=%s&productId=30043";
            HttpRequest httpRequest = new HttpRequest();

            for(int i=0;i<100;i++){
                List<Map<String,String>> crawlerDataList = new ArrayList<>();

                httpRequest.setUrl(String.format(url,i + 1));
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);
                JSONObject pageObj = JSONObject.parseObject(httpPage.getRawText());
                JSONArray jsonArray = pageObj.getJSONObject("result").getJSONArray("commentList");
                for (Object obj : jsonArray) {
                    JSONObject commentObj = (JSONObject)obj;
                    String comment = commentObj.getJSONObject("firstComment").getString("coment");
                    String cid = commentObj.getJSONObject("firstComment").getString("comentId");
                    String uname = commentObj.getString("userName");
                    System.out.println(cid + "----" + uname);
                    System.out.println(comment);
                }
            }

        }

        private void listInfos(Selectable node,Map<String,String> crawlerDataMap){
            List<Selectable> listInforNodes = node.xpath(".//div[@class=\"list_infor\"]/dl[@class=\"clearfix\"]").nodes();
            for (Selectable listInforNode : listInforNodes) {
                String desc = listInforNode.xpath("./dt").get();
                switch (desc){
                    case "爱车车型":
                        crawlerDataMap.put("model_url","https://newcar.xcar.com.cn"+listInforNode.xpath(".//a/@href").get());
                        crawlerDataMap.put("model",listInforNode.xpath(".//a/text()").get());
                    case "购入时间":
                        crawlerDataMap.put("time_buy",listInforNode.xpath("./dd/text()").get());
                    case "购车地点":
                        crawlerDataMap.put("vendor",listInforNode.xpath("./dd/text()").get());
                    case "裸车价格":
                        crawlerDataMap.put("buy_price",listInforNode.xpath("./dd/text()").get());
                    case "当前油耗":
                        crawlerDataMap.put("fuel_economy",listInforNode.xpath("./dd/text()").get());
                }
            }
        }

        private void userScoreInfos(Selectable node,Map<String,String> crawlerDataMap){
            String userName = node.xpath(".//div[@class=\"name_lf\"]/dl[@class=\"clearfix\"]//span/text()").get();
            crawlerDataMap.put("author",userName);

            String scoreTit = node.xpath(".//div[@class=\"name_lf\"]//div[@class=\"score_tit\"]/text()").get()+
                    node.xpath(".//div[@class=\"name_lf\"]//div[@class=\"score_tit\"]/em/text()").get();
            StringBuffer scoreBuffer = new StringBuffer(scoreTit);
            List<Selectable> scoreNodeList = node.xpath(".//div[@class=\"name_lf\"]//ul[@class=\"score_list\"]/li").nodes();
            for (Selectable scoreNodeInfo : scoreNodeList) {
                String score = scoreNodeInfo.xpath("./span/text()").get() + scoreNodeInfo.xpath("./em/text()").get();
                scoreBuffer.append("\n").append(score);
            }
            crawlerDataMap.put("values",scoreBuffer.toString());
        }

        private void otherInfos(Selectable node,Map<String,String> crawlerDataMap){
            List<String> targets = node.xpath(".//div[@class=\"purpose clearfix\"]/em/text()").all();
            crawlerDataMap.put("target", StringUtils.joinWith(",",targets.toArray()));

            List<String> images = node.xpath(".//ul[@class=\"photo_ul\"]//a/img/@src").all();
            crawlerDataMap.put("imageurl",StringUtils.joinWith("\\x01",images.toArray()));

            List<String> content = node.xpath(".//div[@class=\"review_post\"]/dl/*/text()").all();
            crawlerDataMap.put("content",StringUtils.joinWith("",content.toArray()));

            String like = node.xpath(".//a[@class=\"zan_ment\"]/text()").get();
            crawlerDataMap.put("like",like);

            String releaseTime = node.xpath(".//div[@class=\"publish\"]/text()").get();
            crawlerDataMap.put("releaseTime",releaseTime);

            List<String> spans = node.xpath(".//div[@class=\"publish\"]/span/@class").all();
            String isBest=null;
            for (String span : spans) {
                if (span.startsWith("ssen_")){
                    isBest = span;
                }
            }
            crawlerDataMap.put("isBest",isBest);
        }
    }

    static class TestForumRun implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestForumRun(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            String url = "https://www.xcar.com.cn/bbs/viewthread.php?tid=27030255&page=3470";
            HttpRequest httpRequest = new HttpRequest();

            for(int i=0;i<100;i++){
                List<Map<String,String>> crawlerDataList = new ArrayList<>();

                httpRequest.setUrl(String.format(url,Math.random()));
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);
                Html responseHtml = httpPage.getHtml();

                String dataInfo = articleDataInfo(httpPage);
                Json dataInfoJson = new Json(dataInfo);
                String author_name = dataInfoJson.jsonPath($_type + ".author_name").get();
                String author_id = dataInfoJson.jsonPath($_type + ".author_id").get();
                String content_title = dataInfoJson.jsonPath($_type + ".content_title").get();
                String content_id = dataInfoJson.jsonPath($_type + ".content_id").get();



                List<Selectable> nodes = responseHtml.xpath("//div[@class=\"details\"]/div[@class=\"floor_div\"]").nodes();

                for(Selectable node : nodes){

                }
                System.out.println(httpPage.getStatusCode() + "-->" + JSON.toJSONString(crawlerDataList));
            }
        }

        private String articleDataInfo(HttpPage page){
            String dataStr = "data: ";
            String dataInfo = null;
            Matcher mtAuthor = Pattern.compile(dataStr + "\\{([^}])*\\}").matcher(page.getRawText());
            if (mtAuthor.find()){
                dataInfo = mtAuthor.group();
                dataInfo = dataInfo.substring(dataInfo.indexOf(dataStr)+dataStr.length());

            }
            return dataInfo;
        }
    }
}
